/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.easyseata;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import com.easyseata.api.ResultHolder;
import com.easyseata.api.SagaActionOne;
import com.easyseata.api.SagaActionTwo;
import com.easyseata.core.context.RootContext;
import com.easyseata.spring.annotation.GlobalTransactional;


@Service
public class SagaTransactionService {

	@Autowired
    private SagaActionOne sagaActionOne;

	@Autowired
    private SagaActionTwo sagaActionTwo;

    /**
     * 发起分布式事务
     *
     * @return string string
     */
    @GlobalTransactional
    public String doTransactionCommit() {
        //第一个TCC 事务参与者
        boolean result = sagaActionOne.prepare( 1);
        if (!result) {
            throw new RuntimeException("sagaActionOne failed.");
        }
        List<String> list = new ArrayList<>();
        list.add("c1");
        list.add("c2");
        result = sagaActionTwo.prepare(  list);
        if (!result) {
            throw new RuntimeException("sagaActionTwo failed.");
        }
        return RootContext.getXID();
    }

    public void checkBranchTransaction(String xid, boolean commit) {
        String actionOneResult = ResultHolder.getActionOneResult(xid);
        String actionTwoResult = ResultHolder.getActionTwoResult(xid);
        Assert.isTrue(commit ? "T".equals(actionOneResult) : "F".equals(actionOneResult), "分支事务" + (commit ? "提交" : "回滚") + "失败");
        Assert.isTrue(commit ? "T".equals(actionTwoResult) : "F".equals(actionTwoResult), "分支事务" + (commit ? "提交" : "回滚") + "失败");
    }

    /**
     * Do transaction rollback string.
     */
    @GlobalTransactional
    public void doTransactionRollback() {
        //第一个TCC 事务参与者
        boolean result = sagaActionOne.prepare( 1);
        if (!result) {
            throw new RuntimeException("sagaActionOne failed.");
        }
        List<String> list = new ArrayList<>();
        list.add("c1");
        list.add("c2");
        result = sagaActionTwo.prepare( list);
        if (!result) {
            throw new RuntimeException("sagaActionTwo failed.");
        }

        throw new RuntimeException("transaction rollback");
    }

	public void setSagaActionOne(SagaActionOne sagaActionOne) {
		this.sagaActionOne = sagaActionOne;
	}

	public void setSagaActionTwo(SagaActionTwo sagaActionTwo) {
		this.sagaActionTwo = sagaActionTwo;
	}
    
    

}
